package com.adcc.swingui.factory;

import java.awt.Container;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.adcc.swingui.base.DefaultPanel;
import com.adcc.swingui.bean.PanelBean;
import com.adcc.swingui.bean.PanelComponentBean;
import com.adcc.swingui.conf.PanelConf;
import com.jidesoft.docking.DefaultDockableHolder;
import com.jidesoft.docking.DockableFrame;

public class PanelFactory {
	private static Logger logger = Logger.getLogger(PanelFactory.class);
	private static Map<String, Container> map = new HashMap<String, Container>();
	
	public static void create(String[] name, Container container) {
		for (int i = 0; i < name.length; i++) {
			if(map.containsKey(name[i])) {
				logger.warn("面板对象已经存在" + name[i]);
			}
			PanelBean bean = PanelConf.getPanelConf().get(name[i]);
			if(bean != null){
				for(PanelComponentBean component : bean.getPanelList()){
					try{
						Class c = Class.forName(component.getClassName());
						Constructor con = c.getConstructor(new Class[] { Container.class });
						DefaultPanel o = (DefaultPanel) con.newInstance(container);
						((DefaultDockableHolder)container).getDockingManager().getFrame(component.getName()).add(
								o.buildPanel(o.getClass().getName()));
					}catch(Exception e){
						logger.error("工具创建失败" + component.getClassName() + e.getMessage());
					}
				}
			}else{
				logger.error("面板对象配置不存在" + name[i]);
			}
			//菜单缓存到内存
			map.put(name[i], container);
		}
	}
}
